/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.DateTime;

public class NextDepartures {

    private String returnString = "??";

    public NextDepartures(List<ArrivalTime> trackStopTimes) {
        this(trackStopTimes, false);
    }

    public NextDepartures(List<ArrivalTime> trackStopTimes, boolean expected) {
        final int AFTER = 1;

        DateTime now = new DateTime();

        List<String> stopTimes = new ArrayList<String>();
        if (trackStopTimes != null) {
            for (ArrivalTime stopTime: trackStopTimes) {
                Calendar time = expected ? stopTime.getExpectedTime() : stopTime.getScheduledTime();
                
                if (CalendarHourMinuteComparator.INSTANCE.compare(time, now.getCalendar()) == AFTER) {
                    String minute = String.valueOf(time.get(Calendar.MINUTE));
                    if (minute.length() == 1) {
                        minute = "0" + minute;
                    }
                    
                    stopTimes.add(String.format("%s:%s", time.get(Calendar.HOUR_OF_DAY), minute));
                }
            }
        }

        returnString = StringUtils.join(stopTimes, ", ");
    }

    @Override
    public String toString() {
        return returnString;
    }

}
